<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button 
    class="post-btn"
     @click="onPost"
     :disabled='isEnbal'
     >发布</van-button>
  </div>
</template>

<script>
import { addComment } from "@/api/comment";
export default {
  // 依赖注入
  inject:{
    articleId:{
      type:[Number,String,Object],
      default:null
    }
  },
  name: "CommentPost",
  components: {},
  props: {
      target:{
          type:[String,Object,Number],
          required:true
      }
  },
  data() {
    return {
      message: "",
      isEnbal:false
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onPost() {
    this.isEnbal=true
     try{
          const { data } = await addComment({
          target:this.target,
          content:this.message,
          art_id:this.articleId?this.articleId.toString():this.articleId
      })
        this.message=''
    //   关闭弹出框
    // 将发布内容显示到列表顶部
    // 清空文本框


    this.$emit('post-success',data)
    this.$toast.success('发布成功')
    this.isEnbal=false
     }catch(err){
         console.log(err)
         this.$toast.fail('发布失败')
     }
    },
  },
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>